package com.yuncheng.oaApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;


import com.yuncheng.oaApi.constant.OaFlowNodeConstant;
import com.yuncheng.oaApi.entity.OaSysFlowNode;
import com.yuncheng.oaApi.entity.OaSysFlowTemple;
import com.yuncheng.oaApi.entity.OaSysJob;
import com.yuncheng.entity.YcSysOrg;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.mapper.OaSysFlowNodeMapper;
import com.yuncheng.oaApi.service.IOaSysFlowNodeService;
import com.yuncheng.oaApi.service.IOaSysFlowTempleService;
import com.yuncheng.oaApi.service.IOaSysJobService;
import com.yuncheng.oaApi.utils.GetUuIdUtils;
import com.yuncheng.oaApi.vo.Result;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @Description: 流程节点
 * @Author: yuncheng
 * @Date: 2023-10-12
 * @Version: V1.0
 */
@Service
public class OaSysFlowNodeServiceImpl extends BaseServiceImpl<OaSysFlowNodeMapper, OaSysFlowNode> implements IOaSysFlowNodeService {
    private static final Logger log = LoggerFactory.getLogger(OaSysFlowNodeServiceImpl.class);

    @Resource
    @Lazy
    private OaSysFlowNodeMapper oaSysFlowNodeMapper;



    @Resource
    @Lazy
    private OaYcSysOrgServiceImpl ycSysOrgService;

    @Resource
    @Lazy
    private IOaSysJobService sysJobService;

    @Resource
    @Lazy
    private IOaSysFlowNodeService sysFlowNodeService;

    @Resource
    @Lazy
    private IOaSysFlowTempleService sysFlowTempleService;

    //创建第一个节点 初始化 通过模板id 绑定流程模板id
    public Result addFirstNode(String mbId){

        OaSysFlowNode oaSysFlowNode = new OaSysFlowNode();
        oaSysFlowNode.setId(GetUuIdUtils.ReplaceUuId());
        oaSysFlowNode.setFjdmc(OaFlowNodeConstant.FIRST_NODE);
        oaSysFlowNode.setFlcmbid(mbId);
        oaSysFlowNode.setFsyhjid("");
        oaSysFlowNode.setFymid(""); //页面id 默认为空
        oaSysFlowNode.setFblfs(OaFlowNodeConstant.BLFS_DRJJ); //默认多人竞争
        oaSysFlowNode.setFblbmlx("当前部门");//部门名称 默认：当前部门
        oaSysFlowNode.setFblbmid("");  //办理岗位 第一步默认为空
        oaSysFlowNode.setFblgwid(""); //办理岗位 第一步默认为空
        oaSysFlowNode.setFblrid(""); //办理人id 默认为空
        oaSysFlowNode.setFxh(1);
        oaSysFlowNode.setFsfcjdpzhq(0); //是否从节点配置获取待办人 默认 ：是
        oaSysFlowNode.setFsfzdblr(1); //是否指定办理人办理 默认：否
        oaSysFlowNode.setFsfzddlr(0); //是否知道当前登陆人办理 默认：是
        oaSysFlowNodeMapper.insert(oaSysFlowNode);
        return Result.ok(oaSysFlowNode);
    }


    //在次添加节点， 第一个节点在创建模板的时候 自动创建
    public Result addNode(OaSysFlowNode oaSysFlowNode) throws InvocationTargetException, IllegalAccessException {
        //拿到上一个节点的id 赋值给 fsyhjid 上一环节id
        String fblrid = oaSysFlowNode.getFblrid();
        if (StringUtils.isNotBlank(fblrid)){
            String[] split = fblrid.split(",");
            fblrid = split[0];
        }


        //使用Bean 工具类 拷贝
        OaSysFlowNode flowNode = new OaSysFlowNode();
        BeanUtils.copyProperties(flowNode, oaSysFlowNode);
        flowNode.setId(GetUuIdUtils.ReplaceUuId());
        flowNode.setFblrid(fblrid);
//        if (sysFlowNode.getFsfzdblr() == 0){
//            if (fblrid == null){
//                throw new BusinessException("没有选择待办人");
//            }
//        }

        oaSysFlowNodeMapper.insert(flowNode);
       return Result.ok("添加成功");

    }

    /**
     * 当前所有的部门名称
     */
    public List<YcSysOrg> getDepart(){
        LambdaQueryWrapper<YcSysOrg> ycorgWrapper = new LambdaQueryWrapper<>();
                                                    ycorgWrapper.isNotNull(YcSysOrg::getParentId);
        List<YcSysOrg> ycSysOrgList = ycSysOrgService.list(ycorgWrapper);
        return ycSysOrgList;
    }

    /**
     * 当前所有的岗位角色
     */
    public List<OaSysJob> getJob(){
        LambdaQueryWrapper<OaSysJob> jobWrapper = new LambdaQueryWrapper<>();
        jobWrapper.orderByAsc(OaSysJob::getJobOrder);
        List<OaSysJob> oaSysJobList = sysJobService.list(jobWrapper);
        return oaSysJobList;
    }


    /**
     *获取流程名称
     * @param mbId 模板id
     * @return
     */
    public String getLcmc(String mbId){
        OaSysFlowTemple oaSysFlowTemple = sysFlowTempleService.getById(mbId);
        String flcmc = oaSysFlowTemple.getFlcmc();
        return flcmc;
    }

    /**
     *  获取下一个节点 所有的数据
     * @param nowNodeid 当前节点id
     * @return
     */
    public List<OaSysFlowNode> getNextNode(String nowNodeid){
        List<OaSysFlowNode> nextNode = oaSysFlowNodeMapper.selectList(new LambdaQueryWrapper<OaSysFlowNode>()
                .eq(OaSysFlowNode::getFsyhjid, nowNodeid));
        return nextNode;
    }

    /**
     * 获取当前节点的所有数据
     */
    public List<OaSysFlowNode> getNowNode(String nowNodeid){
        List<OaSysFlowNode> nowNodeList = sysFlowNodeService.list(new LambdaQueryWrapper<OaSysFlowNode>().eq(OaSysFlowNode::getId, nowNodeid));
        return nowNodeList;
    }


    /**
     * 获取上一节点的所有数据
     */
    public List<OaSysFlowNode> getPreviousNode(String nowNodeid){
        List<OaSysFlowNode> nowNode = getNowNode(nowNodeid);
        String fsyhjid = nowNode.get(0).getFsyhjid();
        List<OaSysFlowNode> previousNodeList = sysFlowNodeService.list(new LambdaQueryWrapper<OaSysFlowNode>().eq(OaSysFlowNode::getId, fsyhjid));
        if (CollectionUtil.isEmpty(previousNodeList)){
            return null;
        }
        return previousNodeList;

    }


    /**判断当前环节是否是最后一个环节 如果获取下一个环节的size 等于0 当前环节就是最后一个环节
     * true 最后一个环节            false 不是最后一个环节
     * @param nowNodeId 当前环节id
     * @return
     */
    public boolean endNode(String nowNodeId){
        boolean end = false;
        List<OaSysFlowNode> nextNode = getNextNode(nowNodeId);
        if (nextNode.size() == 0){
            end = true;
        }
        return end;
    }

    /**
     * 级联删除
     * @param id
     */
   public void removeId(String id){
     oaSysFlowNodeMapper.deleteById(id);

    }

    /**
     * 获取第一个节点的所有数据
     * @param lcmbid 流程模板id
     * @return
     */
    public List<OaSysFlowNode> getFirstNode(String lcmbid){

        List<OaSysFlowNode> oaSysFlowNodeList = sysFlowNodeService.list(new LambdaQueryWrapper<OaSysFlowNode>()
                .eq(OaSysFlowNode::getFlcmbid, lcmbid).isNull(OaSysFlowNode::getFsyhjid));
        return oaSysFlowNodeList;
    }

    /**
     * 通过流程模板编码获取
     */
    public OaSysFlowTemple getCode(String lcmbCode){
        List<OaSysFlowTemple> list = sysFlowTempleService.list(new LambdaQueryWrapper<OaSysFlowTemple>().eq(OaSysFlowTemple::getFlcbm, lcmbCode));
        if (list == null){
            throw new BusinessException("请配置当前编码流程模板"+"["+lcmbCode+"]");
        }
        return list.get(0);
    }






}
